package top.szhome.framework.core.tools.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * Web请求工具
 *
 * @author 唐加利
 * @date 2023/7/23
 **/
@Slf4j
public class WebUtil {


    /**
     * 获取HttpServletRequest对象
     *
     * @return 返回HttpServletRequest对象
     */
    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        return (requestAttributes == null) ? null : ((ServletRequestAttributes) requestAttributes).getRequest();
    }

    /**
     * 从Request中获取Header信息
     * @param name
     * @return
     */
    public static String getHeader(String name) {
        HttpServletRequest request = getRequest();
        return request.getHeader(name);
    }

    /**
     * 从Request获取头信息列表
     * @param name
     * @return
     */
    public static Enumeration<String> getHeaders(String name) {
        HttpServletRequest request = getRequest();
        return request.getHeaders(name);
    }

    /**
     * 获取请求参数
     * @param name
     * @return
     */
    public static String getParam(String name) {
        HttpServletRequest request = getRequest();
        return request.getParameter(name);
    }

    /**
     * 获取Cookie信息
     * @param name
     * @return
     */
    public static String getCookie(String name){
        HttpServletRequest request = getRequest();
        Cookie[] cookies = request.getCookies();
        if(ObjectUtil.isEmpty(cookies)){
            return null;
        }

        for (Cookie cookie : cookies) {
            if(cookie.getName().equals(name)){
                return cookie.getValue();
            }
        }

        return null;
    }

    /**
     * 从HttpServletRequest中获取属性数据
     * @param name
     * @param <T>
     * @return
     */
    public static <T> T getRequestAttr(String name) {
        HttpServletRequest request = getRequest();
        Object value = request.getAttribute(name);
        return (T) value;
    }

    /**
     * 设置HttpServletRequest属性
     * @param name
     * @param value
     */
    public static void setRequestAttr(String name, Object value) {
        HttpServletRequest request = getRequest();
        request.setAttribute(name, value);
    }

    /**
     * 以JSON格式响应请求
     *
     * @param response
     * @param content
     */
    public static void response(HttpServletResponse response, Object content){
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf8");
        try (PrintWriter out = response.getWriter()) {
            out.append(JsonUtil.toJson(content));
        } catch (IOException e) {
            log.error("响应JSON消息失败, content={}", JsonUtil.toJson(content));
        }
    }

}
